<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="roomsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="房间ID" align="center" prop="roomId"/>
      <el-table-column label="楼栋" align="center" prop="buildingType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.building_type" :value="scope.row.buildingType"/>
        </template>
      </el-table-column>
      <el-table-column label="楼层" align="center" prop="floor"/>
      <el-table-column label="房间号" align="center" prop="roomNumber"/>
      <el-table-column label="房间类型）" align="center" prop="roomType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.room_type" :value="scope.row.roomType"/>
        </template>
      </el-table-column>
      <el-table-column label="房间容纳人数" align="center" prop="capacity"/>
      <el-table-column label="每晚价格" align="center" prop="pricePerNight"/>
      <el-table-column label="房间设施" align="center" prop="amenities"/>
      <el-table-column label="房间图片" align="center" prop="imgUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imgUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="房间状态）" align="center" prop="roomStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.room_status" :value="scope.row.roomStatus"/>
        </template>
      </el-table-column>

      <el-table-column label="用户姓名" align="center" prop="userName"/>
      <el-table-column label="用户身份证" align="center" prop="userIdCard"/>
      <el-table-column label="用户入住日期" align="center" prop="userCheckinDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.userCheckinDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <!--如果当前日期大于用户入住日期，则显示结账按钮-->
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="toCheckout(scope.row)"
          >结账
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="checkOut(scope.row)"
          >退房
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="checkout.title" :visible.sync="checkout.open" width="500px" append-to-body>
      <!--支付宝收款码-->
      <div style="text-align: center">请支付{{ checkout.money }}元</div>
      <image-preview :src="checkout.imgUrl" :width="50" :height="50"/>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="toPay(checkout.id)">确 定</el-button>
        <el-button @click="checkout.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listRooms, getRooms, delRooms, addRooms, updateRooms} from "@/api/hotefl/rooms";

export default {
  name: "Rooms",
  dicts: ['building_type', 'room_type', 'room_status'],
  data() {
    return {
      // 预览
      pv: {
        id: '',
        open: false
      },
      // checkout
      checkout: {
        money: 0,
        id: '',
        open: false,
        title: "扫码付款",
        imgUrl: this.$store.getters.avatar,
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房间管理表格数据
      roomsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        buildingType: null,
        floor: null,
        roomNumber: null,
        roomType: null,
        capacity: null,
        pricePerNight: null,
        amenities: null,
        imgUrl: null,
        roomStatus: null,
        userId: null,
        userName: null,
        userIdCard: null,
        userCheckinDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        buildingType: [
          {required: true, message: "楼栋不能为空", trigger: "change"}
        ],
        floor: [
          {required: true, message: "楼层不能为空", trigger: "blur"}
        ],
        roomNumber: [
          {required: true, message: "房间号不能为空", trigger: "blur"}
        ],
        roomType: [
          {required: true, message: "房间类型）不能为空", trigger: "change"}
        ],
        capacity: [
          {required: true, message: "房间容纳人数不能为空", trigger: "blur"}
        ],
        pricePerNight: [
          {required: true, message: "每晚价格不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 退房 */
    checkOut(row) {
      let data = row;
      data.userId = null;
      data.roomStatus = 0;
      data.userCheckinDate = null;
      data.userName = "无";
      data.userIdCard = "无"
      this.$confirm('是否确认退房?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(
        () => {
          updateRooms(data).then(response => {
            this.$modal.msgSuccess("退房成功");
          });
        }
      ).finally(
        () => {
          this.getList();
        }
      )
    },
    preview(typeId) {
      this.pv.id = typeId;
      listRooms(this.queryParams).then(response => {
        this.queryParams.buildingType = typeId;
        this.queryParams.userId = this.$store.getters.userId;
        this.queryParams.pageSize = 200;
        this.roomsList = response.rows;
        this.total = response.total;
        this.pv.open = true;
      });
    },
    /** 查询房间管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.userId = this.$store.getters.userId;
      listRooms(this.queryParams).then(response => {
        this.roomsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        roomId: null,
        buildingType: null,
        floor: null,
        roomNumber: null,
        roomType: null,
        capacity: null,
        pricePerNight: null,
        amenities: null,
        imgUrl: null,
        roomStatus: null,
        userId: null,
        userName: null,
        userIdCard: null,
        userCheckinDate: null
      };
      this.resetForm("form");
    },
    toCheckout(row) {
      const roomId = row.roomId || this.ids
      getRooms(roomId).then(response => {
        this.form = response.data;
        this.checkout.open = true;
        this.checkout.id = roomId;
        this.checkout.money = roomId;
        this.checkout.title = "扫描支付";
        // 计算金额 当前时间- 入住时间 * 房间价格
        let number = new Date().getTime() - new Date(this.form.userCheckinDate).getTime();
        // 转天数
        number = number / (1000 * 60 * 60 * 24);
        this.checkout.money = (number * this.form.pricePerNight).toFixed(1);
      });
      this.reset();
    },
    toPay(roomId) {
      getRooms(roomId).then(response => {
        this.form = response.data;
        // 状态默认可用
        this.form.roomStatus = 0;
        // 这里默认填写写管理员id
        this.form.userId = null;
        this.form.userName = "等待阿姨打扫";
        this.form.userIdCard = "等待阿姨打扫";
        this.form.userCheckinDate = null;
        updateRooms(this.form).then(response => {
          this.$modal.msgSuccess("支付成功");
          this.getList();
          this.checkout.open = false;
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.roomId != null) {
            updateRooms(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            // 状态默认可用
            this.form.roomStatus = 0;
            // 这里默认填写写管理员id
            this.form.userId = null;
            this.form.userName = "房间创建成功,请打扫";
            this.form.userIdCard = "房间创建成功,请打扫";
            addRooms(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>
